'use strict'
const path = require('path')
import { app, protocol, BrowserWindow,screen,Menu,MenuItem} from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'
// import {startServer,stopServer} from "../server"
Menu.setApplicationMenu(null)
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
]);
function initMenu() {
  const menu = new Menu()
  menu.append(
    new MenuItem({
      label: '太蓝云桌面',
      submenu: [
        {
          role: '全屏',
          accelerator: 'F11',
          click: () => {
            //全屏快捷键
            win.setFullScreen(!win.isFullScreen())
            win.webContents.send('window-fullsize', win.isMaximized() ? true : win.isFullScreen())
          }
        }
      ]
    })
  )

  Menu.setApplicationMenu(menu)
}

initMenu();

const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, commandLine, workingDirectory) => {
   
    if (win) {
      if (win.isMinimized()) win.restore()
          win.focus()
      // mainWindow.flashFrame(true)
    }
  })
}

let win=null;
function getAutoSize(num){
  let size = screen.getPrimaryDisplay().workAreaSize
  let width = parseInt(size.width)
  let height = parseInt(size.height)
  let calc =(width/height);
  let WINW=parseInt(width*num);
  let WINH=parseInt(height-(width*(1-num))/calc);
  
  return {
    width:WINW,
    height:WINH
  }
}
async function createWindow() {
   let size1 = getAutoSize(0.9);
   let size2 =getAutoSize(0.7);
   win = new BrowserWindow({
    width:size1.width,
    height:size1.height,
    minWidth:size2.width,
    minHeight:size2.height,
    show:false,
    autoHideMenuBar:true,
    // titleBarStyle: 'hidden',
    // frame: false,
    webPreferences: {
      webSecurity:false,//禁止同源策略
      allowRunningInsecureContent:true,
      contextIsolation:true,
      preload:path.resolve(__dirname,"preload.js"),
      nodeIntegrationInWorker: true,
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    win.webContents.openDevTools()
    // if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol("app");
    // startServer().then(()=>{
    //   win.loadURL('http://localhost:3000/index.html');
    // });
    // win.webContents.openDevTools()
    // win.loadFile(`${__dirname}/index.html`);
    //createProtocol('app')
    win.loadURL('app://./index.html');
    win.webContents.openDevTools()
  }
}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }else{
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
});
app.on('ready', async () => {
  createWindow();
  win.webContents.session.clearStorageData({
    storages:['cookies','localstorage','cachestorage']
  });
  win.webContents.setZoomFactor(1);
  win.once('ready-to-show', () => {
    
     win.show();
  })
})

if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}
